module planegame{

    export class Background extends egret.Sprite {

        private static BACKGROUND_SCROLL_SPEED = 3;

        private bg1: egret.Bitmap;
        private bg2: egret.Bitmap;

        private timer: egret.Timer;

        public constructor(resName:string) {
            super();

            this.bg1 = new egret.Bitmap();
            this.bg2 = new egret.Bitmap();

            let texture: egret.Texture = RES.getRes(resName);
            this.bg1.texture = texture;
            this.bg2.texture = texture;

            this.width = texture.textureWidth;
            this.height = texture.textureHeight;
            
            this.addChild(this.bg1);
            this.addChild(this.bg2);
            this.resetPosition();
            
            this.timer = new egret.Timer(1000/60,0);

            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        }

        private onAddToStage(event: egret.Event) {
            this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.end, this);

            // timer
            this.timer.addEventListener(egret.TimerEvent.TIMER, this.onTimeUpdate, this);
            //this.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.onTimeComplete, this);
            //this.timer.start();
        }

        // timer
        private onTimeUpdate():void{
            this.bg1.x -= Background.BACKGROUND_SCROLL_SPEED;
            this.bg2.x -= Background.BACKGROUND_SCROLL_SPEED;
            if(this.bg1.x <= -this.bg1.width) {
                this.bg1.x = this.bg2.x + this.bg2.width;
            }
            if(this.bg2.x <= -this.bg2.width) {
                this.bg2.x = this.bg1.x + this.bg1.width;
            }
        }

        // recycle
        public end() {
            this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.end, this);
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

            // timer
            if(this.timer.running) {
                this.timer.stop();
            }
            if(this.timer.hasEventListener(egret.TimerEvent.TIMER)) {
                this.timer.removeEventListener(egret.TimerEvent.TIMER, this.onTimeUpdate, this);
            }
            
        }

        public startScroll() {
            if(!this.timer.running) {
                this.timer.start();
            }
        }

        public stopScroll() {
            if(this.timer.running) {
                this.timer.stop();
            }
        }

        public resetPosition() {
            this.bg1.x = 0;
            this.bg1.y = 0;
            this.bg2.x = this.bg1.x+this.bg1.width;
            this.bg2.y = this.bg1.y;
        }

        public setFillStage(stageWidth:number, stageHeight:number) {
            this.bg1.width = stageWidth;
            this.bg1.height = stageHeight;

            this.bg2.width = stageWidth;
            this.bg2.height = stageHeight;

            this.resetPosition();
        }
    }
}